﻿/**************************************************************************
创建时间:	2020/5/15 11:49:23    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Win.Controls;
using ZhCun.Win.Extend;

namespace ZhCun.Win.MenuRole
{
    public partial class FrmRoleMenu : FrmBase
    {
        public FrmRoleMenu()
        {
            InitializeComponent();
        }

        readonly MenuRoleBLL BLLObj = new MenuRoleBLL();

        private void FrmRoleMenu_Load(object sender, EventArgs e)
        {
            InitRole();
            InitTree();
            InitRights();
        }

        private void InitRights()
        {
            if (IsManager)
            {
                ToolStripEx.LoopItem(SetRights, tsRole, tsTree, tsRoleMenu);

                void SetRights(ToolStripItem tsItem)
                {
                    if (!tsItem.Enabled)
                    {
                        tsItem.Enabled = tsItem.Visible = true;
                    }
                }
            }
        }


        #region 角色维护

        Sys_Role _CurrRole;

        void InitRole()
        {
            dgvRole.SelectionChangedAndCellClick += DgvRole_SelectionChangedAndCellClick;

            RefrashRoleData();
        }

        private void DgvRole_SelectionChangedAndCellClick(object sender, DataGridViewCellEventArgs e)
        {
            _CurrRole = dgvRole.GetSelectedClassData<Sys_Role>();
            RefrashTreeData();
        }

        void RefrashRoleData()
        {
            dgvRole.DataSource = BLLObj.RoleGetData();
            _CurrMenuTree = null;
            RefrashTreeData();
        }

        private void tsBtnRoleRefrash_Click(object sender, EventArgs e)
        {
            RefrashRoleData();
        }

        private void tsBtnRoleAdd_Click(object sender, EventArgs e)
        {
            FrmRoleEdit frm = new FrmRoleEdit();
            var r =
            frm.ShowDialog(new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.RoleAdd(m as Sys_Role),
                SaveModel = new Sys_Role()
            });
            if (r)
            {
                ShowMessage(r.msg);
                RefrashRoleData();
            }


            //ShowInput(Save, "角色新增");
            //ApiResult Save(string input)
            //{
            //    var r = BLLObj.RoleAdd(new Sys_Role { RoleName = input });
            //    if (r)
            //    {
            //        ShowMessage(r.msg);
            //        RefrashRoleData();
            //    }
            //    return r;
            //}
        }

        private void tsBtnRoleEdit_Click(object sender, EventArgs e)
        {
            var selRole = dgvRole.GetSelectedClassData<Sys_Role>();
            if (selRole == null)
            {
                ShowMessage("未选中编辑记录");
                return;
            }

            FrmRoleEdit frm = new FrmRoleEdit();
            var r =
            frm.ShowDialog(new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.RoleEdit(m as Sys_Role),
                SaveModel = selRole,
                SetModel = selRole
            });
            if (r)
            {
                ShowMessage(r.msg);
                RefrashRoleData();
            }
            
            //ShowInput(Save, "角色编辑", selRole.RoleName);
            //ApiResult Save(string input)
            //{
            //    selRole.RoleName = input;
            //    var r = BLLObj.RoleEdit(selRole);
            //    if (r)
            //    {
            //        ShowMessage(r.msg);
            //        RefrashRoleData();
            //    }
            //    return r;
            //}
        }

        private void tsBtnRoleDel_Click(object sender, EventArgs e)
        {
            var selRole = dgvRole.GetSelectedClassData<Sys_Role>();
            if (selRole == null)
            {
                ShowMessage("未选中任何记录");
                return;
            }
            if (!ShowQuestion("确实要删除当前选中角色吗?", "删除确认"))
            {
                return;
            }

            var ret = BLLObj.RoleDel(selRole);
            if (ret)
            {
                RefrashRoleData();
            }
            ShowMessage(ret.msg);
        }


        #endregion

        #region 角色菜单节点

        Sys_Role_Menu _CurrMenuTree;

        readonly string _ToolBarNodeName = "ToolBarNodeName";

        void InitTree()
        {
            tvRoleMenu.AllowDropNode = true;
            tvRoleMenu.DropNodeBefore += TvRoleMenu_DropNodeBefore;
            tvRoleMenu.AfterSelect += TvRoleMenu_AfterSelect;
        }

        private void TvRoleMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Name == _ToolBarNodeName)
            {
                _CurrMenuTree = null;
                RefrashRoleMenuData(true);
                return;
            }

            var selModel = e.Node.Tag as Sys_Role_Menu;
            if (selModel.IsMenu) return;
            _CurrMenuTree = selModel;
            RefrashRoleMenuData();
        }

        void RefrashTreeData()
        {
            RefrashTreeData(tvRoleMenu);
        }

        void RefrashTreeData(TreeViewEx tv)
        {
            if (_CurrRole != null)
            {
                var treeList = BLLObj.TreeGetData(_CurrRole.Id);
                tv.InitTreeViewData(treeList, NodeAddHandle);
                void NodeAddHandle(TreeNode node, Sys_Role_Menu m)
                {
                    if (tv == tvRoleMenu)
                    {
                        node.ContextMenuStrip = cmNodeOperate;
                    }

                    if (m.IsMenu)
                    {
                        node.ImageIndex = 1;
                        node.SelectedImageIndex = 1;
                    }
                }

                var toolNode = tv.Nodes.Add(_ToolBarNodeName, "工具栏菜单");
                toolNode.SelectedImageIndex = 2;
                toolNode.ImageIndex = 2;
                tv.ExpandAll();
            }
            else
            {
                tv.Nodes.Clear();
            }
            dgvRoleMenu.DataSource = null;
        }

        private void tsBtnTreeRefrash_Click(object sender, EventArgs e)
        {
            RefrashTreeData();
        }

        private void tsBtnTreeAddTop_Click(object sender, EventArgs e)
        {
            if (_CurrRole == null)
            {
                ShowMessage("请选中角色");
                return;
            }

            //增加顶级菜单导航分类
            if (ShowInput(Save, "新增顶级分类"))
            {
                ShowMessage("新增顶级成功");
                RefrashTreeData();
            }

            ApiResult Save(string input)
            {
                var r =
                BLLObj.TreeAdd(new Sys_Role_Menu
                {
                    RoleId = _CurrRole.Id,
                    IsMenu = false,
                    MenuCaption = input,
                    OrderNo = tvRoleMenu.Nodes.Count,
                    PId = string.Empty,
                });
                if (r)
                {
                    ShowMessage(r.msg);
                }
                return r;
            }
        }

        private void tsMenuTreeAddSub_Click(object sender, EventArgs e)
        {
            var selNode = tvRoleMenu.SelectedNode;
            Sys_Role_Menu selModel = selNode.Tag as Sys_Role_Menu;
            //增加顶级菜单导航分类
            ShowInput(Save, $"新增 {selNode.FullPath} 的子级分类");

            ApiResult Save(string input)
            {
                var r =
                BLLObj.TreeAdd(new Sys_Role_Menu
                {
                    RoleId = selModel.RoleId,
                    IsMenu = false,
                    MenuCaption = input,
                    OrderNo = tvRoleMenu.Nodes.Count,
                    PId = selNode.Name
                });
                if (r)
                {
                    ShowMessage(r.msg);
                    RefrashTreeData();
                }

                return r;
            }
        }

        private void tsMenuTreeEdit_Click(object sender, EventArgs e)
        {
            var selNode = tvRoleMenu.SelectedNode;
            //增加顶级菜单导航分类
            if (ShowInput(Save, $"编辑 {selNode.Text} 节点", selNode.Text))
            {
                ShowMessage("修改成功");
                RefrashTreeData();
            }

            ApiResult Save(string input)
            {
                return
                BLLObj.TreeEdit(new Sys_Role_Menu
                {
                    MenuCaption = input,
                    Id = selNode.Name
                });
            }
        }

        private void tsMenuTreeDel_Click(object sender, EventArgs e)
        {
            if (ShowQuestion("确实要移除当前节点吗?", "移除确认"))
            {
                var selNode = tvRoleMenu.SelectedNode;
                var ret = BLLObj.TreeDel(selNode.Tag as Sys_Role_Menu);
                if (ret)
                {
                    RefrashTreeData();
                }
                ShowMessage(ret.msg);
            }
        }

        private void tsMenuTreeSetTop_Click(object sender, EventArgs e)
        {
            if (ShowQuestion("确实要设置为顶级节点吗?", "设置确认"))
            {
                TreeNode selNode = tvRoleMenu.SelectedNode;
                var selModel = selNode.Tag as Sys_Role_Menu;
                selModel.PId = string.Empty;
                selModel.OrderNo = tvRoleMenu.Nodes.Count;
                var ret =
                BLLObj.TreeEdit(selModel);
                if (ret)
                {
                    RefrashTreeData();
                }
                ShowMessage(ret.msg);
            }
        }

        private bool TvRoleMenu_DropNodeBefore(TreeNode moveNode, TreeNode targetNode)
        {
            if (targetNode.ImageIndex == 1)
            {
                ShowMessage("不能拖动到菜单项");
                return false;
            }
            if (targetNode.Name == _ToolBarNodeName)
            {
                ShowMessage("不能拖到工具栏");
                return false;
            }

            var m = moveNode.Tag as Sys_Role_Menu;
            m.OrderNo = targetNode.Nodes.Count;
            m.PId = targetNode.Name;
            var r = BLLObj.TreeEdit(m);
            ShowMessage(r.msg);
            return !!r;
        }

        #endregion

        #region 角色菜单

        void RefrashRoleMenuData(bool isToolMenuData = false)
        {
            if (_CurrMenuTree == null && !isToolMenuData)
            {
                dgvRoleMenu.DataSource = null;
            }
            else
            {
                var dt = BLLObj.GetRoleMenus(_CurrRole.Id, _CurrMenuTree?.Id, tsTxtMenuSearch.Text, isToolMenuData);
                dgvRoleMenu.DataSource = dt;
            }
        }

        void InitMenuTreeData(TreeViewEx tv)
        {
            var menuList = BLLObj.GetMenuTree();
            tv.InitTreeViewData(menuList, AddedNode);
            tv.ImageList = this.imageList1;
            void AddedNode(TreeNode node, Sys_Menu menu)
            {
                if (menu.IsMenu)
                {
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                }
            }
        }

        private void tsRoleMneuAdd_Click(object sender, EventArgs e)
        {
            var selNode = tvRoleMenu.SelectedNode;
            if (selNode == null)
            {
                ShowMessage("请选择一个菜单分类");
                return;
            }
            Sys_Role_Menu setModel = new Sys_Role_Menu();
            setModel.PId = selNode.Name;
            setModel.PCaption = selNode.FullPath;
            setModel.RoleId = _CurrRole.Id;
            setModel.RoleName = _CurrRole.RoleName;
            var frm = new FrmRoleMenuEdit();
            frm.InitMenuTreeHandle = InitMenuTreeData;
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (menu) => BLLObj.AddRoleMenu(menu as Sys_Role_Menu),
                SaveModel = setModel,
                SetModel = setModel
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                RefrashRoleMenuData();
                RefrashTreeData();
                tvRoleMenu.SetSelectedNode(selNode.Name);
            }
        }

        private void tsBtnRoleMenuEdit_Click(object sender, EventArgs e)
        {
            var selMenu = dgvRoleMenu.GetSelectedClassData<Sys_Role_Menu>();
            if (selMenu == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var selNode = tvRoleMenu.SelectedNode;
            selMenu.PCaption = selNode.FullPath;
            selMenu.RoleName = _CurrRole.RoleName;
            selMenu.RoleId = _CurrRole.Id;
            var frm = new FrmRoleMenuEdit();
            frm.InitMenuTreeHandle = InitMenuTreeData;
            frm.Text += " 修改";
            var arg = new EditFormArgs
            {
                SaveHandle = (menu) => BLLObj.EditRoleMenu(menu as Sys_Role_Menu),
                SaveModel = selMenu,
                SetModel = selMenu
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                RefrashRoleMenuData();
                RefrashTreeData();
                tvRoleMenu.SetSelectedNode(selNode.Name);
            }
        }

        private void tsBtnRoleMenuDel_Click(object sender, EventArgs e)
        {
            var selMenu = dgvRoleMenu.GetSelectedClassData<Sys_Role_Menu>();
            if (selMenu == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            if (!ShowQuestion("确实要删除当前选中记录吗？", "删除确认"))
            {
                return;
            }
            var ret = BLLObj.DelRoleMenu(selMenu);
            if (ret)
            {
                RefrashRoleMenuData();
                RefrashTreeData();
                tvRoleMenu.SetSelectedNode(selMenu.PId);
            }
            ShowMessage(ret.msg);
        }

        private void tsTxtMenuSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tsBtnSearch_Click(sender, e);
            }
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            RefrashRoleMenuData();
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}